#import "RHLanguageSettingVC.h"
#import "NSBundle+Language.h"
#import "RHTabBarController.h"
#import "MJRefreshConfig.h"
#import "UITableViewCell+Indicator.h"
#import "NetworkConfig.h"

#define tabHeight 50
@interface RHLanguageSettingVC ()<UITableViewDelegate,UITableViewDataSource>


@property (strong, nonatomic)  UITableView *tableView;
@property(nonatomic,strong) NSArray *languageArr;
@property(nonatomic,copy)NSString *languageString;//
@end
@implementation RHLanguageSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    self.navigationItem.title = @"语言".rh_localized;
//    self.languageArr = @[@"中文".rh_localized,@"英文".rh_localized,@"法语".rh_localized,@"德语".rh_localized,@"日语".rh_localized,@"意大利语".rh_localized,@"西班牙语".rh_localized];
    self.languageArr = @[@"中文",@"English",@"Español",@"Français",@"Polski",@"Deutsch",@"日本語"];
   
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, kUI_WINDOW_W-40, self.languageArr.count*tabHeight)];
    self.tableView.rowHeight = tabHeight;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.tableView.bounces = NO;
  //获取手动已设置的语言
    if ([kUserDefaults objectForKey:kLanguage]) {
        self.languageString = [kUserDefaults objectForKey:kLanguage];
    }else{
        self.languageString = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
    }
   
   
   //获取当前语言,返回app已配置的语言  lporj文件夹
   NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
   NSLog(@"当前系统语言1:%@",preferredLanguage);
   
   //获取手机系统当前设置的语言和地区的代码
   NSString *currentLanguageRegion = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];
    NSLog(@"当前系统语言2:%@",currentLanguageRegion);
    
    UIBarButtonItem *rightBarButton = [UIBarButtonItem itemWithTitle:@"确定".rh_localized titleColor:[UIColor blackColor] Target:self Seletor:@selector(sure)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    self.tableView.layer.cornerRadius = 15;
    self.tableView.layer.masksToBounds = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



#pragma mark ------ 确定修改 ---------
- (void)sure{
    
    [self.navigationController popViewControllerAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
            [self changeLanguageTo:self.languageString];
        
    });
    
}
- (void)changeLanguageTo:(NSString *)language {
    // 设置语言
    [NSBundle setLanguage:language];
    
    // 然后将设置好的语言存储好，下次进来直接加载
    [kUserDefaults setObject:language forKey:kLanguage];
    [kUserDefaults synchronize];
    //设置下拉刷新语言
    MJRefreshConfig.defaultConfig.languageCode = language;
    //设置下拉刷新语言
    NSBundle *mjBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]];
    MJRefreshConfig.defaultConfig.i18nBundle = mjBundle;
    MJRefreshConfig.defaultConfig.languageCode = language;
    
    if([language containsString:@"zh"]){
        //图片选择器的语言
        [[NSUserDefaults standardUserDefaults] setObject:@(ZLLanguageChineseSimplified) forKey:@"ZLLanguage"];
    }else{
        //图片选择器的语言
        [[NSUserDefaults standardUserDefaults] setObject:@(ZLLanguageEnglish) forKey:@"ZLLanguage"];
    }
    
    //后台语言
    [NetworkConfig sharedConfig].language = language;
    // 我们要把系统windown的rootViewController替换掉
    RHTabBarController *tabVC = [[RHTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabVC;
    tabVC.selectedIndex = 3;
//    UIViewController *selectVC =  tabVC.selectedViewController;
//    if ([selectVC isKindOfClass:[UINavigationController class]]) {
//        UINavigationController *nav = (UINavigationController*)tabVC.selectedViewController;
//        [nav pushViewController:[[RHLanguageSettingVC alloc]init] animated:NO];
//        
//    }
    NSLog(@"------>更改语言为:%@",language);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.languageArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.backgroundColor = UIColor.whiteColor;
    cell.contentView.backgroundColor = UIColor.whiteColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.languageArr[indexPath.row];
    cell.textLabel.textColor = kCOLOR_HEX(@"#171A1D");
    [cell changeIndicatorView:@""];
    //选中图标
    
    if (indexPath.row==0&&[self.languageString isEqualToString:@"zh-Hans"]) {
       [cell changeIndicatorView:@"agree"];
    }else if(indexPath.row == 1&&[self.languageString isEqualToString:@"en"]){
        [cell changeIndicatorView:@"agree"];
    }else if(indexPath.row == 2&&[self.languageString isEqualToString:@"es"]){
        [cell changeIndicatorView:@"agree"];
    }else if(indexPath.row == 3&&[self.languageString isEqualToString:@"fr"]){
        [cell changeIndicatorView:@"agree"];
    }else if(indexPath.row == 4&&[self.languageString isEqualToString:@"pl"]){
        [cell changeIndicatorView:@"agree"];
    }
    else if(indexPath.row == 5&&[self.languageString isEqualToString:@"de"]){
        [cell changeIndicatorView:@"agree"];
    }else if(indexPath.row == 6&&[self.languageString isEqualToString:@"ja"]){
        [cell changeIndicatorView:@"agree"];
    }
    
    if (indexPath.row !=self.languageArr.count - 1) {
        UIView *line = kView;
        line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
        [cell addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.right.mas_offset(-20);
            make.height.offset(1);
            make.bottom.mas_offset(0);
        }];
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        self.languageString = @"zh-Hans";
    }else if (indexPath.row==1){
        self.languageString = @"en";
    }else if (indexPath.row==2){
        self.languageString = @"es";//西班牙语
    }else if (indexPath.row==3){
        self.languageString = @"fr";//法语
    }else if (indexPath.row==4){
        self.languageString = @"pl";//波兰语
    }
    else if (indexPath.row==5){
        self.languageString = @"de";//德语
    }else if (indexPath.row==6){
        self.languageString = @"ja";//日语
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
